package com.aisile.shop.service;

import com.aisile.pojo.TbSeller;
import com.aisile.sellergoods.service.SellerService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;


import java.util.ArrayList;
import java.util.List;
@Service
public class UserServiceImpl implements UserDetailsService {

    SellerService sellerService;

    //setter注入
    public void setSellerService(SellerService sellerService) {
        this.sellerService = sellerService;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        List<GrantedAuthority> grantedList = new ArrayList<>();
        grantedList.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        TbSeller tbSeller = sellerService.findById(username);
        if (tbSeller != null) {
            if ("1".equals(tbSeller.getStatus())) {
                return new User(username, tbSeller.getPassword(), grantedList);
            }
        }
        return null;
    }

}
